Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 lines
16 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # anotify.py
  4. # Copyright (c) 2012 magnific0 <jacco.geul@gmail.com>
  5. #
  6. # based on:
  7. # growl.py
  8. # Copyright (c) 2011 Sorin Ionescu <sorin.ionescu@gmail.com>
  9. #
  10. # Permission is hereby granted, free of charge, to any person obtaining a copy
  11. # of this software and associated documentation files (the "Software"), to deal
  12. # in the Software without restriction, including without limitation the rights
  13. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. # copies of the Software, and to permit persons to whom the Software is
  15. # furnished to do so, subject to the following conditions:
  16. #
  17. # The above copyright notice and this permission notice shall be included in
  18. # all copies or substantial portions of the Software.
  19. #
  20. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. # SOFTWARE.
  27. SCRIPT_NAME = 'anotify'
  28. SCRIPT_AUTHOR = 'magnific0'
  29. SCRIPT_VERSION = '1.0.2'
  30. SCRIPT_LICENSE = 'MIT'
  31. SCRIPT_DESC = 'Sends libnotify notifications upon events.'
  32. # Changelog
  33. # 2014-05-10: v1.0.1 Change hook_print callback argument type of
  34. # displayed/highlight (WeeChat >= 1.0)
  35. # 2012-09-20: v1.0.0 Forked from original and adapted for libnotify.
  36. # -----------------------------------------------------------------------------
  37. # Settings
  38. # -----------------------------------------------------------------------------
  39. SETTINGS = {
  40. 'show_public_message': 'off',
  41. 'show_private_message': 'on',
  42. 'show_public_action_message': 'off',
  43. 'show_private_action_message': 'on',
  44. 'show_notice_message': 'off',
  45. 'show_invite_message': 'on',
  46. 'show_highlighted_message': 'on',
  47. 'show_server': 'on',
  48. 'show_channel_topic': 'on',
  49. 'show_dcc': 'on',
  50. 'show_upgrade_ended': 'on',
  51. 'sticky': 'off',
  52. 'sticky_away': 'on',
  53. 'icon': '/usr/share/pixmaps/weechat.xpm',
  54. }
  55. # -----------------------------------------------------------------------------
  56. # Imports
  57. # -----------------------------------------------------------------------------
  58. try:
  59. import re
  60. import os
  61. import weechat
  62. import notify2
  63. IMPORT_OK = True
  64. except ImportError as error:
  65. IMPORT_OK = False
  66. if str(error).find('weechat') != -1:
  67. print('This script must be run under WeeChat.')
  68. print('Get WeeChat at http://www.weechat.org.')
  69. else:
  70. weechat.prnt('', 'anotify: {0}'.format(error))
  71. # -----------------------------------------------------------------------------
  72. # Globals
  73. # -----------------------------------------------------------------------------
  74. TAGGED_MESSAGES = {
  75. 'public message or action': set(['irc_privmsg', 'notify_message']),
  76. 'private message or action': set(['irc_privmsg', 'notify_private']),
  77. 'notice message': set(['irc_notice', 'notify_private']),
  78. 'invite message': set(['irc_invite', 'notify_highlight']),
  79. 'channel topic': set(['irc_topic', ]),
  80. #'away status': set(['away_info', ]),
  81. }
  82. UNTAGGED_MESSAGES = {
  83. 'away status':
  84. re.compile(r'^You ((\w+).){2,3}marked as being away', re.UNICODE),
  85. 'dcc chat request':
  86. re.compile(r'^xfer: incoming chat request from (\w+)', re.UNICODE),
  87. 'dcc chat closed':
  88. re.compile(r'^xfer: chat closed with (\w+)', re.UNICODE),
  89. 'dcc get request':
  90. re.compile(
  91. r'^xfer: incoming file from (\w+) [^:]+: ((?:,\w|[^,])+),',
  92. re.UNICODE),
  93. 'dcc get completed':
  94. re.compile(r'^xfer: file ([^\s]+) received from \w+: OK', re.UNICODE),
  95. 'dcc get failed':
  96. re.compile(
  97. r'^xfer: file ([^\s]+) received from \w+: FAILED',
  98. re.UNICODE),
  99. 'dcc send completed':
  100. re.compile(r'^xfer: file ([^\s]+) sent to \w+: OK', re.UNICODE),
  101. 'dcc send failed':
  102. re.compile(r'^xfer: file ([^\s]+) sent to \w+: FAILED', re.UNICODE),
  103. }
  104. DISPATCH_TABLE = {
  105. 'away status': 'set_away_status',
  106. 'public message or action': 'notify_public_message_or_action',
  107. 'private message or action': 'notify_private_message_or_action',
  108. 'notice message': 'notify_notice_message',
  109. 'invite message': 'notify_invite_message',
  110. 'channel topic': 'notify_channel_topic',
  111. 'dcc chat request': 'notify_dcc_chat_request',
  112. 'dcc chat closed': 'notify_dcc_chat_closed',
  113. 'dcc get request': 'notify_dcc_get_request',
  114. 'dcc get completed': 'notify_dcc_get_completed',
  115. 'dcc get failed': 'notify_dcc_get_failed',
  116. 'dcc send completed': 'notify_dcc_send_completed',
  117. 'dcc send failed': 'notify_dcc_send_failed',
  118. }
  119. STATE = {
  120. 'icon': None,
  121. 'is_away': False
  122. }
  123. # -----------------------------------------------------------------------------
  124. # Notifiers
  125. # -----------------------------------------------------------------------------
  126. def cb_irc_server_connected(data, signal, signal_data):
  127. '''Notify when connected to IRC server.'''
  128. if weechat.config_get_plugin('show_server') == 'on':
  129. a_notify(
  130. 'Server',
  131. 'Server Connected',
  132. 'Connected to network {0}.'.format(signal_data))
  133. return weechat.WEECHAT_RC_OK
  134. def cb_irc_server_disconnected(data, signal, signal_data):
  135. '''Notify when disconnected to IRC server.'''
  136. if weechat.config_get_plugin('show_server') == 'on':
  137. a_notify(
  138. 'Server',
  139. 'Server Disconnected',
  140. 'Disconnected from network {0}.'.format(signal_data))
  141. return weechat.WEECHAT_RC_OK
  142. def cb_notify_upgrade_ended(data, signal, signal_data):
  143. '''Notify on end of WeeChat upgrade.'''
  144. if weechat.config_get_plugin('show_upgrade_ended') == 'on':
  145. a_notify(
  146. 'WeeChat',
  147. 'WeeChat Upgraded',
  148. 'WeeChat has been upgraded.')
  149. return weechat.WEECHAT_RC_OK
  150. def notify_highlighted_message(prefix, message):
  151. '''Notify on highlighted message.'''
  152. if weechat.config_get_plugin("show_highlighted_message") == "on":
  153. a_notify(
  154. 'Highlight',
  155. 'Highlighted Message',
  156. "{0}: {1}".format(prefix, message),
  157. priority=notify2.URGENCY_CRITICAL)
  158. def notify_public_message_or_action(prefix, message, highlighted):
  159. '''Notify on public message or action.'''
  160. if prefix == ' *':
  161. regex = re.compile(r'^(\w+) (.+)$', re.UNICODE)
  162. match = regex.match(message)
  163. if match:
  164. prefix = match.group(1)
  165. message = match.group(2)
  166. notify_public_action_message(prefix, message, highlighted)
  167. else:
  168. if highlighted:
  169. notify_highlighted_message(prefix, message)
  170. elif weechat.config_get_plugin("show_public_message") == "on":
  171. a_notify(
  172. 'Public',
  173. 'Public Message',
  174. '{0}: {1}'.format(prefix, message))
  175. def notify_private_message_or_action(prefix, message, highlighted):
  176. '''Notify on private message or action.'''
  177. regex = re.compile(r'^CTCP_MESSAGE.+?ACTION (.+)$', re.UNICODE)
  178. match = regex.match(message)
  179. if match:
  180. notify_private_action_message(prefix, match.group(1), highlighted)
  181. else:
  182. if prefix == ' *':
  183. regex = re.compile(r'^(\w+) (.+)$', re.UNICODE)
  184. match = regex.match(message)
  185. if match:
  186. prefix = match.group(1)
  187. message = match.group(2)
  188. notify_private_action_message(prefix, message, highlighted)
  189. else:
  190. if highlighted:
  191. notify_highlighted_message(prefix, message)
  192. elif weechat.config_get_plugin("show_private_message") == "on":
  193. a_notify(
  194. 'Private',
  195. 'Private Message',
  196. '{0}: {1}'.format(prefix, message))
  197. def notify_public_action_message(prefix, message, highlighted):
  198. '''Notify on public action message.'''
  199. if highlighted:
  200. notify_highlighted_message(prefix, message)
  201. elif weechat.config_get_plugin("show_public_action_message") == "on":
  202. a_notify(
  203. 'Action',
  204. 'Public Action Message',
  205. '{0}: {1}'.format(prefix, message),
  206. priority=notify2.URGENCY_NORMAL)
  207. def notify_private_action_message(prefix, message, highlighted):
  208. '''Notify on private action message.'''
  209. if highlighted:
  210. notify_highlighted_message(prefix, message)
  211. elif weechat.config_get_plugin("show_private_action_message") == "on":
  212. a_notify(
  213. 'Action',
  214. 'Private Action Message',
  215. '{0}: {1}'.format(prefix, message),
  216. priority=notify2.URGENCY_NORMAL)
  217. def notify_notice_message(prefix, message, highlighted):
  218. '''Notify on notice message.'''
  219. regex = re.compile(r'^([^\s]*) [^:]*: (.+)$', re.UNICODE)
  220. match = regex.match(message)
  221. if match:
  222. prefix = match.group(1)
  223. message = match.group(2)
  224. if highlighted:
  225. notify_highlighted_message(prefix, message)
  226. elif weechat.config_get_plugin("show_notice_message") == "on":
  227. a_notify(
  228. 'Notice',
  229. 'Notice Message',
  230. '{0}: {1}'.format(prefix, message))
  231. def notify_invite_message(prefix, message, highlighted):
  232. '''Notify on channel invitation message.'''
  233. if weechat.config_get_plugin("show_invite_message") == "on":
  234. regex = re.compile(
  235. r'^You have been invited to ([^\s]+) by ([^\s]+)$', re.UNICODE)
  236. match = regex.match(message)
  237. if match:
  238. channel = match.group(1)
  239. nick = match.group(2)
  240. a_notify(
  241. 'Invite',
  242. 'Channel Invitation',
  243. '{0} has invited you to join {1}.'.format(nick, channel))
  244. def notify_channel_topic(prefix, message, highlighted):
  245. '''Notify on channel topic change.'''
  246. if weechat.config_get_plugin("show_channel_topic") == "on":
  247. regex = re.compile(
  248. r'^\w+ has (?:changed|unset) topic for ([^\s]+)' +
  249. '(?:(?: from "(?:(?:"\w|[^"])+)")? to "((?:"\w|[^"])+)")?',
  250. re.UNICODE)
  251. match = regex.match(message)
  252. if match:
  253. channel = match.group(1)
  254. topic = match.group(2) or ''
  255. a_notify(
  256. 'Channel',
  257. 'Channel Topic',
  258. "{0}: {1}".format(channel, topic))
  259. def notify_dcc_chat_request(match):
  260. '''Notify on DCC chat request.'''
  261. if weechat.config_get_plugin("show_dcc") == "on":
  262. nick = match.group(1)
  263. a_notify(
  264. 'DCC',
  265. 'Direct Chat Request',
  266. '{0} wants to chat directly.'.format(nick))
  267. def notify_dcc_chat_closed(match):
  268. '''Notify on DCC chat termination.'''
  269. if weechat.config_get_plugin("show_dcc") == "on":
  270. nick = match.group(1)
  271. a_notify(
  272. 'DCC',
  273. 'Direct Chat Ended',
  274. 'Direct chat with {0} has ended.'.format(nick))
  275. def notify_dcc_get_request(match):
  276. 'Notify on DCC get request.'
  277. if weechat.config_get_plugin("show_dcc") == "on":
  278. nick = match.group(1)
  279. file_name = match.group(2)
  280. a_notify(
  281. 'DCC',
  282. 'File Transfer Request',
  283. '{0} wants to send you {1}.'.format(nick, file_name))
  284. def notify_dcc_get_completed(match):
  285. 'Notify on DCC get completion.'
  286. if weechat.config_get_plugin("show_dcc") == "on":
  287. file_name = match.group(1)
  288. a_notify('DCC', 'Download Complete', file_name)
  289. def notify_dcc_get_failed(match):
  290. 'Notify on DCC get failure.'
  291. if weechat.config_get_plugin("show_dcc") == "on":
  292. file_name = match.group(1)
  293. a_notify('DCC', 'Download Failed', file_name)
  294. def notify_dcc_send_completed(match):
  295. 'Notify on DCC send completion.'
  296. if weechat.config_get_plugin("show_dcc") == "on":
  297. file_name = match.group(1)
  298. a_notify('DCC', 'Upload Complete', file_name)
  299. def notify_dcc_send_failed(match):
  300. 'Notify on DCC send failure.'
  301. if weechat.config_get_plugin("show_dcc") == "on":
  302. file_name = match.group(1)
  303. a_notify('DCC', 'Upload Failed', file_name)
  304. # -----------------------------------------------------------------------------
  305. # Utility
  306. # -----------------------------------------------------------------------------
  307. def set_away_status(match):
  308. status = match.group(1)
  309. if status == 'been ':
  310. STATE['is_away'] = True
  311. if status == 'longer ':
  312. STATE['is_away'] = False
  313. def cb_process_message(
  314. data,
  315. wbuffer,
  316. date,
  317. tags,
  318. displayed,
  319. highlight,
  320. prefix,
  321. message
  322. ):
  323. '''Delegates incoming messages to appropriate handlers.'''
  324. tags = set(tags.split(','))
  325. functions = globals()
  326. is_public_message = tags.issuperset(
  327. TAGGED_MESSAGES['public message or action'])
  328. buffer_name = weechat.buffer_get_string(wbuffer, 'name')
  329. dcc_buffer_regex = re.compile(r'^irc_dcc\.', re.UNICODE)
  330. dcc_buffer_match = dcc_buffer_regex.match(buffer_name)
  331. highlighted = False
  332. if int(highlight):
  333. highlighted = True
  334. # Private DCC message identifies itself as public.
  335. if is_public_message and dcc_buffer_match:
  336. notify_private_message_or_action(prefix, message, highlighted)
  337. return weechat.WEECHAT_RC_OK
  338. # Pass identified, untagged message to its designated function.
  339. for key, value in UNTAGGED_MESSAGES.items():
  340. match = value.match(message)
  341. if match:
  342. functions[DISPATCH_TABLE[key]](match)
  343. return weechat.WEECHAT_RC_OK
  344. # Pass identified, tagged message to its designated function.
  345. for key, value in TAGGED_MESSAGES.items():
  346. if tags.issuperset(value):
  347. functions[DISPATCH_TABLE[key]](prefix, message, highlighted)
  348. return weechat.WEECHAT_RC_OK
  349. return weechat.WEECHAT_RC_OK
  350. def a_notify(notification, title, description, priority=notify2.URGENCY_LOW):
  351. '''Returns whether notifications should be sticky.'''
  352. is_away = STATE['is_away']
  353. icon = STATE['icon']
  354. time_out = 5000
  355. if weechat.config_get_plugin('sticky') == 'on':
  356. time_out = 0
  357. if weechat.config_get_plugin('sticky_away') == 'on' and is_away:
  358. time_out = 0
  359. try:
  360. notify2.init("wee-notifier")
  361. wn = notify2.Notification(title, description, icon)
  362. wn.set_urgency(priority)
  363. wn.set_timeout(time_out)
  364. wn.show()
  365. except Exception as error:
  366. weechat.prnt('', 'anotify: {0}'.format(error))
  367. # -----------------------------------------------------------------------------
  368. # Main
  369. # -----------------------------------------------------------------------------
  370. def main():
  371. '''Sets up WeeChat notifications.'''
  372. # Initialize options.
  373. for option, value in SETTINGS.items():
  374. if not weechat.config_is_set_plugin(option):
  375. weechat.config_set_plugin(option, value)
  376. # Initialize.
  377. name = "WeeChat"
  378. icon = "/usr/share/pixmaps/weechat.xpm"
  379. notifications = [
  380. 'Public',
  381. 'Private',
  382. 'Action',
  383. 'Notice',
  384. 'Invite',
  385. 'Highlight',
  386. 'Server',
  387. 'Channel',
  388. 'DCC',
  389. 'WeeChat'
  390. ]
  391. STATE['icon'] = icon
  392. # Register hooks.
  393. weechat.hook_signal(
  394. 'irc_server_connected',
  395. 'cb_irc_server_connected',
  396. '')
  397. weechat.hook_signal(
  398. 'irc_server_disconnected',
  399. 'cb_irc_server_disconnected',
  400. '')
  401. weechat.hook_signal('upgrade_ended', 'cb_upgrade_ended', '')
  402. weechat.hook_print('', '', '', 1, 'cb_process_message', '')
  403. if __name__ == '__main__' and IMPORT_OK and weechat.register(
  404. SCRIPT_NAME,
  405. SCRIPT_AUTHOR,
  406. SCRIPT_VERSION,
  407. SCRIPT_LICENSE,
  408. SCRIPT_DESC,
  409. '',
  410. ''
  411. ):
  412. main()