Class: Exmail::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/exmail.rb

Constant Summary collapse

DOMAIN_TOKEN =

主要用于获取token

'exmail.qq.com'
DOMAIN_API =

主要是调用API

'openapi.exmail.qq.com:12211'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_name, key) ⇒ Server

Returns a new instance of Server.



12
13
14
# File 'lib/exmail.rb', line 12

def initialize(, key)
  @account_name, @key=, key
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



6
7
8
# File 'lib/exmail.rb', line 6

def access_token
  @access_token
end

#account_nameObject

Returns the value of attribute account_name.



6
7
8
# File 'lib/exmail.rb', line 6

def 
  @account_name
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/exmail.rb', line 6

def key
  @key
end

Instance Method Details

#change_pass(email, new_pass) ⇒ Object

修改密码new_pass为新密码



59
60
61
# File 'lib/exmail.rb', line 59

def change_pass(email, new_pass)
  user_sync({:alias => email, :password => new_pass, :md5 => 0, :action => 3})
end

#get_tokenObject

返回数据: ”token_type“:”Bearer“, ”expires_in“:86400, ”refresh_token“:”“



160
161
162
163
164
165
166
167
168
169
# File 'lib/exmail.rb', line 160

def get_token
  url_path = '/cgi-bin/token'
  url = "https://#{DOMAIN_TOKEN}#{url_path}"
  json_str = RestClient.post(url, {:grant_type => 'client_credentials',
                                   :client_id => @account_name,
                                   :client_secret => @key})
  json = JSON.load(json_str)
  @access_token = json['access_token']
  json
end

#group_add(group_name, group_admin, status, members) ⇒ Object

添加邮件群组group-admin==群组管理者(需要使用一个域中不存在的 邮箱地址) status–群组状态(分为 4 类 all,inner,group, list)



134
135
136
137
138
# File 'lib/exmail.rb', line 134

def group_add(group_name, group_admin, status, members)
  api_path='/openapi/group/add'
  post_api(api_path, {:group_name => group_name, :group_admin => group_admin,
                      :status => status, :members => members})
end

#group_add_member(group_alias, members) ⇒ Object

添加邮件群组成员



148
149
150
151
# File 'lib/exmail.rb', line 148

def group_add_member(group_alias, members)
  api_path = '/openapi/group/addmember'
  post_api(api_path, {:group_alias => group_alias, :members => members})
end

#group_del_member(group_alias, members) ⇒ Object

删除邮件群组成员



154
155
156
157
# File 'lib/exmail.rb', line 154

def group_del_member(group_alias, members)
  api_path = '/openapi/group/deletemember'
  post_api(api_path, {:group_alias => group_alias, :members => members})
end

#group_delete(group_alias) ⇒ Object

删除邮件群组group_alias–群组管理员(一个域中不存在的邮箱地址)



142
143
144
145
# File 'lib/exmail.rb', line 142

def group_delete(group_alias)
  api_path='/openapi/group/delete'
  post_api(api_path, {:group_alias => group_alias})
end

#party_add(party_name) ⇒ Object

创建部门多级部门用/分隔,例如: /IT部/IT产品部



77
78
79
# File 'lib/exmail.rb', line 77

def party_add(party_name)
  party_sync(:action => 2, :dstpath => party_name)
end

#party_add_p(party_name) ⇒ Object

自动创建所有层的部门类似于 mkdir_p party_add_p(‘aa/bb/cc/dd’)将创建四级部门



84
85
86
87
88
89
90
91
# File 'lib/exmail.rb', line 84

def party_add_p(party_name)
  _party_path = ''
  party_name.split('/').each do |party|
    next if party.to_s==''
    party_add("#{_party_path}/#{party}") unless party_exists?("#{_party_path}/#{party}")
    _party_path+=('/'+party)
  end
end

#party_delete(party_name) ⇒ Object

删除部门多级部门用/分隔,例如: /IT部/IT产品部



95
96
97
# File 'lib/exmail.rb', line 95

def party_delete(party_name)
  party_sync(:action => 1, :dstpath => party_name)
end

#party_exists?(party_path) ⇒ Boolean

是否存在该部门

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/exmail.rb', line 118

def party_exists?(party_path)
  result = party_list(party_path)
  result['error']!='party_not_found'
end

#party_list(partypath = nil) ⇒ Object

获取子部门



112
113
114
115
# File 'lib/exmail.rb', line 112

def party_list(partypath=nil)
  api_path='/openapi/party/list'
  post_api(api_path, {:partypath => partypath})
end

#party_mod(party_name, parent_party) ⇒ Object

修改部门party_mod(‘/北京’,‘/IT部/IT产品部’) 将“/IT部/IT产品部”移动到根目录下,并改名为“北京”



101
102
103
# File 'lib/exmail.rb', line 101

def party_mod(party_name, parent_party)
  party_sync(:action => 3, :dstpath => party_name, :srcpath => parent_party)
end

#party_sync(params) ⇒ Object

同步部门信息



106
107
108
109
# File 'lib/exmail.rb', line 106

def party_sync(params)
  api_path = '/openapi/party/sync'
  post_api(api_path, params)
end

#partyuser_list(partypath = nil) ⇒ Object

获取部门下成员获取不存在的部门的子部门返回 “errcode”=>“1310” 正常结果 “List”=>}



126
127
128
129
# File 'lib/exmail.rb', line 126

def partyuser_list(partypath=nil)
  api_path = '/openapi/partyuser/list'
  post_api(api_path, {:partypath => partypath})
end

#post_api(api_path, params) ⇒ Object



171
172
173
174
175
# File 'lib/exmail.rb', line 171

def post_api(api_path, params)
  get_token if @access_token.nil?
  url = "http://#{DOMAIN_API}#{api_path}"
  JSON.load RestClient.post(url, params, {:Authorization => "Bearer #{@access_token}"})
end

#user_add(email, user_attr) ⇒ Object

创建用户slave别名列表 # 1. 如果多个别名,传多个 Slave 2. Slave 上限为 5 个 3. Slave 为邮箱格式user_add(‘[email protected]’,:position=>“”, :partypath=>“北京/IT技术部”, :gender=>1,

:password=>"123123", :extid=>1, :mobile=>"", :md5=>0, :tel=>"", :name=>"dede", :opentype=>1)


40
41
42
43
44
45
46
47
48
49
# File 'lib/exmail.rb', line 40

def user_add(email, user_attr)
  params = user_attr.merge(:action => 2, :alias => email)
  result = user_sync(params)
  if !result.nil? && result['error']=='party_not_found'
    partypath = user_attr[:partypath]||user_attr['partypath']
    party_add_p(partypath)
    sleep(1) #添加完部门要等一会才会有
    user_sync(params)
  end
end

#user_check(emails) ⇒ Object

检查邮件帐号是否可用,多个邮箱用数组形式传入user_check() 返回 “List”=>[{“Email”=>“[email protected]”, “Type”=>1, “Type”=>1]} Type说明: -1:帐号名无效 0: 帐号名没被占用 1:主帐号名 2:别名帐号 3:邮件群组帐 号



30
31
32
33
34
# File 'lib/exmail.rb', line 30

def user_check(emails)
  emails=[emails] if emails.class!=Array
  api_path = '/openapi/user/check'
  post_api(api_path, 'email='+emails.join('&email='))
end

#user_delete(email) ⇒ Object

删除用户



64
65
66
# File 'lib/exmail.rb', line 64

def user_delete(email)
  user_sync(:action => 1, :alias => email)
end

#user_get(email) ⇒ Object

获取具体的邮箱的信息返回数据: “List”=>},

"OpenType"=>1, "Name"=>"bruce", "Mobile"=>"18612345678", "Status"=>1, "Tel"=>"",
"Position"=>"IT技术", "Gender"=>1, "SlaveList"=>"", "Alias"=>"[email protected]", "ExtId"=>""}


20
21
22
23
24
# File 'lib/exmail.rb', line 20

def user_get(email)
  api_path = '/openapi/user/get'
  result = post_api(api_path, {:alias => email})
  result['error'].nil? ? result : {}
end

#user_mod(email, user_attr) ⇒ Object

修改用户,调用方法参考 user_add



52
53
54
55
# File 'lib/exmail.rb', line 52

def user_mod(email, user_attr)
  params = user_attr.merge(:action => 3, :alias => email)
  user_sync(params)
end

#user_sync(params) ⇒ Object

同步成员资料Action string 1=DEL, 2=ADD, 3=MOD



70
71
72
73
# File 'lib/exmail.rb', line 70

def user_sync(params)
  api_path = '/openapi/user/sync'
  post_api(api_path, params)
end